﻿using System;
using System.Collections.Generic;
 
using System.Text;
using System.Runtime.InteropServices;


namespace HDF5
{
    using hid_t = System.Int32;
    using herr_t = System.Int32;
    using hsize_t = System.UInt64;
    using hssize_t = System.Int64;
    using hunsigned = System.UInt32;

    using hbool_t = System.UInt32;
    using htri_t = System.Int32;
    using ssize_t = System.Int64; 

    using haddr_t = System.UInt64;

    using uint32_t = System.UInt32;
    using uint64_t = System.UInt64;

    using H5O_msg_crt_idx_t = System.UInt32;

    using size_t = System.UInt32;

    using off_t = System.Int32;

    using H5E_major_t = System.Int32;
    using H5E_minor_t = System.Int32;



    #region enums
    /* The difference between a single file and a set of mounted files */
    public enum H5F_scope_t {
        H5F_SCOPE_LOCAL	= 0,	/*specified file handle only		*/
        H5F_SCOPE_GLOBAL	= 1 	/*entire virtual file			*/
    }  

     /* How does file close behave?
     * H5F_CLOSE_DEFAULT - Use the degree pre-defined by underlining VFL
     * H5F_CLOSE_WEAK    - file closes only after all opened objects are closed
     * H5F_CLOSE_SEMI    - if no opened objects, file is close; otherwise, file
		           close fails
     * H5F_CLOSE_STRONG  - if there are opened objects, close them first, then
		           close file
     */
    public enum H5F_close_degree_t {
        H5F_CLOSE_DEFAULT   = 0,
        H5F_CLOSE_WEAK      = 1,
        H5F_CLOSE_SEMI      = 2,
        H5F_CLOSE_STRONG    = 3
    } 

    

    /*
     * Types of allocation requests. The values larger than H5FD_MEM_DEFAULT
     * should not change other than adding new types to the end. These numbers
     * might appear in files.
     */
    public enum H5F_mem_t {
        H5FD_MEM_NOLIST	= -1,			/*must be negative*/
        H5FD_MEM_DEFAULT	= 0,			/*must be zero*/
        H5FD_MEM_SUPER      = 1,
        H5FD_MEM_BTREE      = 2,
        H5FD_MEM_DRAW       = 3,
        H5FD_MEM_GHEAP      = 4,
        H5FD_MEM_LHEAP      = 5,
        H5FD_MEM_OHDR       = 6,

        H5FD_MEM_NTYPES				/*must be last*/
    }  

    /* Library's file format versions */
    public enum H5F_libver_t {
        H5F_LIBVER_EARLIEST,        /* Use the earliest possible format for storing objects */
        H5F_LIBVER_LATEST           /* Use the latest possible format available for storing objects*/
    }

    #endregion enums


    #region structs
    /* Current "global" information about file */
    /* (just size info currently) */
    public struct H5F_info_t
    {
        hsize_t super_ext_size;	/* Superblock extension size */
        struct sohm
        {
            hsize_t hdr_size;       /* Shared object header message header size */
            H5_ih_info_t msgs_info;      /* Shared object header message index & heap size */
        } ;
    }
    #endregion structs

    public static class H5F
    {

        /*
         * These are the bits that can be passed to the `flags' argument of
         * H5Fcreate() and H5Fopen(). Use the bit-wise OR operator (|) to combine
         * them as needed.  As a side effect, they call H5check_version() to make sure
         * that the application is compiled with a version of the hdf5 header files
         * which are compatible with the library to which the application is linked.
         * We're assuming that these constants are used rather early in the hdf5
         * session.
         *
         */
        //Changed from UInt32 to UInt16(unsigned short)
        public static System.UInt32 H5F_ACC_RDONLY	=  0x0000;//	/*absence of rdwr => rd-only */
        public static System.UInt32 H5F_ACC_RDWR =	 0x0001;	/*open for read and write    */
        public static System.UInt32 H5F_ACC_TRUNC =	 0x0002;	/*overwrite existing files   */
        public static System.UInt32 H5F_ACC_EXCL =	 0x0004;	/*fail if file already exists*/
        public static System.UInt32 H5F_ACC_DEBUG =	 0x0008;	/*print debug info	     */
        public static System.UInt32 H5F_ACC_CREAT =	 0x0010;	/*create non-existing files  */

        /* Value passed to H5Pset_elink_acc_flags to cause flags to be taken from the
         * parent file. */
        public static System.UInt32 H5F_ACC_DEFAULT = 0xffff;	/*ignore setting on lapl     */


        /* Flags for H5Fget_obj_count() & H5Fget_obj_ids() calls */
        public static System.UInt32 H5F_OBJ_FILE	= 0x0001;       /* File objects */
        public static System.UInt32 H5F_OBJ_DATASET	= 0x0002;       /* Dataset objects */
        public static System.UInt32 H5F_OBJ_GROUP=	0x0004;       /* Group objects */
        public static System.UInt32 H5F_OBJ_DATATYPE = 0x0008;      /* Named datatype objects */
        public static System.UInt32 H5F_OBJ_ATTR =   0x0010;       /* Attribute objects */
        public static System.UInt32 H5F_OBJ_ALL =	(UInt16)(H5F_OBJ_FILE|H5F_OBJ_DATASET|H5F_OBJ_GROUP|H5F_OBJ_DATATYPE|H5F_OBJ_ATTR);
        public static System.UInt32 H5F_OBJ_LOCAL = 0x0020;       /* Restrict search to objects opened through current file ID */
                                                /* (as opposed to objects opened through any file ID accessing this file) */

        public static hsize_t H5F_UNLIMITED = unchecked((hsize_t)(-1L));
        public static hsize_t H5F_FAMILY_DEFAULT = 0;

        public static string H5F_MPIO_DEBUG_KEY = "H5F_mpio_debug_key";




        // imports
        #region File functions
        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern hid_t H5Fcreate([In][MarshalAs(UnmanagedType.LPStr)] string name, hunsigned flags, hid_t fcpl_id, hid_t fapl_id )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern  hid_t H5Fopen( [In][MarshalAs(UnmanagedType.LPStr)] string name, hunsigned flags, hid_t fapl_id ) ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern hid_t H5Freopen(hid_t file_id )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Fclose( hid_t file_id )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Fflush(hid_t object_id, H5F_scope_t scope )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern  htri_t H5Fis_hdf5([In][MarshalAs(UnmanagedType.LPStr)] string name ) ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Fmount(hid_t loc_id, [In][MarshalAs(UnmanagedType.LPStr)] string name, hid_t child_id, hid_t plist_id )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Funmount(hid_t loc_id, [In][MarshalAs(UnmanagedType.LPStr)] string name )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Fget_vfd_handle(hid_t file_id, hid_t fapl_id, IntPtr[] file_handle )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Fget_filesize(hid_t file_id, out hsize_t size )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern  hid_t H5Fget_create_plist(hid_t file_id ) ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern hid_t H5Fget_access_plist(hid_t file_id)  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Fget_info( hid_t obj_id, ref H5F_info_t file_info )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern  herr_t H5Fget_intent(hid_t file_id, out hunsigned intent) ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern ssize_t H5Fget_name(hid_t obj_id, StringBuilder name, size_t size )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern ssize_t H5Fget_obj_count( hid_t file_id, hunsigned types )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern     ssize_t H5Fget_obj_ids( hid_t file_id, hunsigned  types, size_t max_objs, out hid_t obj_id_list )  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern  hssize_t H5Fget_freespace(hid_t file_id)  ;

        //[DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        //public static extern herr_t H5Fget_mdc_config(hid_t file_id, H5AC_cache_config_t *config_ptr)  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Fget_mdc_hit_rate(hid_t file_id, out double hit_rate_ptr)  ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern  herr_t H5Fget_mdc_size(hid_t file_id, out size_t max_size_ptr, out size_t min_clean_size_ptr,out size_t cur_size_ptr,out int cur_num_entries_ptr) ;

        [DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        public static extern herr_t H5Freset_mdc_hit_rate_stats(hid_t file_id)  ;

        //[DllImport("hdf5dll.dll",CharSet=CharSet.Ansi)]
        //public static extern  herr_t H5Fset_mdc_config(hid_t file_id, H5AC_cache_config_t *config_ptr) ;

        #endregion File functions


    }
}
